import * as qiniu from 'qiniu-js'
import request from "@/api/utils/request.js";
// const qiniu = require('qiniu-js')
const apiFileGetVideoToken = async() => await request.get("/getVideoToken")
const apiFileGetImageToken = async() => await request.get("/getImageToken")


export const  pushinfo= async (dto) => await  request.post("video/uploadVideo",dto)
// 为什么下面的这种不可以提交呀
// export  function pushinfo(dto) {
//     return  request({
//         url: '/uploadVideo',  // URL作为第一个参数
//         methods:'post',
//         data: dto  // 请求体数据作为第二个参数
//         }
//
//     )
// }



const Config = {
    qiniuOSS: {}  // 七牛云配置(当前为空)
}


/**
 * 上传文件
 * 文件上传是通过后端拿到上传的凭证token后在前端进行文件的上传
 */
export const apiUploadVideo = async (file, callBack={next:()=>{},error:()=>{}, complete:()=>{}})=> {
    // 先获取文件的token
    const fileToken = (await apiFileGetVideoToken()).token
    const observable = qiniu.upload(file, null, fileToken, {}, Config.qiniuOSS)
    const subscription = observable.subscribe(callBack.next, callBack.error, callBack.complete)
    // subscription.unsubscribe() // 上传取消
    return subscription;
}



export const apiUploadImage = async (file, callBack={next:()=>{},error:()=>{}, complete:()=>{}})=> {
    // 先获取文件的token
    const fileToken = (await apiFileGetImageToken()).token
    const observable = qiniu.upload(file, null, fileToken, {}, Config.qiniuOSS)
    const subscription = observable.subscribe(callBack.next, callBack.error, callBack.complete)
    // subscription.unsubscribe() // 上传取消
    return subscription;
}


// export const getMsg = async () => {
//   await request.get("/data");
// }

export const getMsg = async ()=>{
        let res=   await request.get("/data")
    return res;
}


// apiPushFile(e.key, (fileId)=>callBack.complete(e,fileId))
// 在上面用了
const apiPushFile = async (fileKey, callback=()=>{})=>{
    let result = (await request.post(`/file`,null, {
        params: {
            fileKey
        }
    }))
    callback(result.data)

}

/**
 * 查看文件
 * @param {int} fileId 文件id
 * @returns
 */
export const apiFileGet = (fileId)=>{
    return request.getUri({
        url: `/file/${fileId}`
    })
}
